#!/bin/sh

. scripts/functions

#
# Run a simple backup using the compressed option
#   then backup a second time and finally restore it
#
TestName="exclude-dir-test"
JobName=exclude-dir
# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

scripts/cleanup
scripts/copy-test-confs

cat >> $conf/susan-dir.conf <<EOF
FileSet {
  Name = "ExcludeDir"
  Include {
    Exclude Dir Containing = ".nobkp"
    Options {
      signature=MD5
      compression=GZIP
    }
    File = "$BackupDirectory"
  }
  Include {
    Exclude Dir Containing = "susan-dir.conf"
    Options {
      signature=MD5
      compression=GZIP
    }
    File = "$conf"
    File = "$rscripts"
    File = "${BackupDirectory}/build/po"
  }
}
EOF
sed 's/FileSet="CompressedSet"/FileSet=ExcludeDir/' $conf/susan-dir.conf >$tmp/1
cp -f $tmp/1 $conf/susan-dir.conf
change_jobname CompressedTest $JobName

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/flat-c.tgz

mkdir -p ${BackupDirectory}/build/po
echo "test" > ${BackupDirectory}/build/po/fr.po
mkdir -p ${BackupDirectory}/build/src/xxtestdir
touch ${BackupDirectory}/build/src/xxtestdir/.nobkp
cp ${BackupDirectory}/*.c ${BackupDirectory}/build/src/xxtestdir


start_test

cat >tmp/bconcmds <<END_OF_DATA
@$out /dev/null
messages
@$out $tmp/log3.out
estimate job=$JobName listing
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName yes
wait
messages
@$out $tmp/log2.out
restore where=${cwd}/tmp/susan-restores select all storage=File done
yes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File
stop_susan

check_two_logs

# should be ok if we remove the xxtestdir
rm -rf ${BackupDirectory}/build/src/xxtestdir
check_restore_diff ${BackupDirectory}

grep '/xxtestdir' $tmp/log3.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Should not see xxtestdir in estimate output"
    bstat=2
fi

# build/po is included 2 times with a different exclude file
# we shouldn't see scripts and bin
nb=`egrep -c 'build/po/fr.po$' $tmp/log3.out`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should see fr.po two times"
    bstat=2
fi

grep $rscripts $tmp/log3.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Should not $rscripts in estimate output"
    bstat=2
fi

grep $conf $tmp/log3.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Should not $conf in estimate output"
    bstat=2
fi

end_test
